<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="仓库名称" prop="stordocName">
                <el-input v-model="ruleForm.stordocName" :placeholder="'请输入仓库名称'" clearable />
            </el-form-item>
            <el-form-item label="仓库地址" prop="address">
                <el-input v-model="ruleForm.address" :placeholder="'请输入仓库地址'" clearable />
            </el-form-item>
            <el-form-item label="负责人" prop="personNo">
                <el-select v-model="ruleForm.personNo" class="w100p" clearable filterable placeholder="请选择负责人">
                    <el-option v-for="item, index in person" :key="index" :label="item.person.name"
                        :value="item.person.personNo"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="仓库类型" prop="stordocType">
                <el-select v-model="ruleForm.stordocType" class="w100p" clearable filterable placeholder="请选择仓库类型">
                    <el-option label="原料" value="原料"></el-option>
                    <el-option label="半成品" value="半成品"></el-option>
                    <el-option label="成品" value="成品"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否启用" prop="enableStatus">
                <el-select v-model="ruleForm.enableStatus" class="w100p" clearable filterable placeholder="请选择仓库类型">
                    <el-option label="未启用" :value="false"></el-option>
                    <el-option label="已启用" :value="true"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="电话号码" prop="phone">
                <el-input v-model="ruleForm.phone" :placeholder="'请输入电话号码'" clearable />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="ruleForm.remark" :placeholder="'请输入备注'" clearable />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import warehouseManage from '@/api/digtalOperation/warehouseManage';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: warehouseManage.add,
            ruleForm: {},
            rules: {
                phone: [
                    { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
                    {
                        pattern: /^1[3-9]\d{9}$/, // 手机号正则表达式，这里假设只验证中国大陆手机号  
                        message: '手机号格式不正确',
                        trigger: ['blur', 'change']
                    }
                ],
                enableStatus: [
                    { required: true, message: '请选择是否启用', trigger: 'change' }
                ],
                stordocType: [
                    { required: true, message: '请选择仓库类型', trigger: 'change' }
                ],
                personNo: [
                    { required: true, message: '请选择负责人', trigger: 'change' }
                ],
                address: [
                    { required: true, message: '请输入仓库地址', trigger: 'change' }
                ],
                stordocName: [
                    { required: true, message: '请输入仓库名称', trigger: 'change' }
                ]
            }
        }
    },
    computed: {
        person() {
            return this.$store.state.initData.person || [];
        },
        stordoc() {
            return this.$store.state.initData.stordoc || [];
        },
    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    mounted() {
    },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.ruleForm = {};
                this.addEditApi = warehouseManage.addStordoc
                this.addEditName = '添加';
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data };
                this.addEditApi = warehouseManage.editStordoc
                this.addEditName = '编辑';
            };
        },
        //提交表单
        submitForm() {

            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.person.forEach(item => {
                        if (item.personNo == this.ruleForm.personNo) {
                            this.$set(this.ruleForm, 'personName', item.personName)
                        }
                    })
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        if (res.code == 1) {
                            this.resetForm();
                            this.$message.success('操作成功')
                        } else {
                            this.$message.warning('操作失败')
                        }
                        this.cancel()
                    }).catch(err => {
                        this.$message.warning('操作失败')
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.tag {
    margin: 5px 0;
    margin-right: 5px;
    cursor: pointer;
}
</style>
  